/* CONSTRUCTING THE STOCK OF BRAND CAPITAL based on Perpetual Inventory Method */

set more off
clear all


** Define paths
local raw_data_path = "...\Data" // path to the raw data files: compustat, crsp monthly
local output_path = "...\results" // path to the results folder

* Set the path to raw_data_path
cd "`raw_data_path'"

/* Parameters */

* Top brand capital portfolio or long-short ?
local top_brandcap_pf = 0 // 1: for top brand capital ; 0: long-short portfolio of top brands capital


* Paramters to estimate brand capital

local delta = 0.2		/* Depreciation rate: Belo et al (RED2014) used 50% whereas Vitorino MS2014 and Belo et al (JFE2022) uses 20%.  */
local g = 0.1			/* Average real growth rate of firm-level advertising expenses. Both Belo RED2014 and Vitorino MS2014 uses 10%. in Belo et al (JFE) its industry specific but they do not mention specifically which numbers they used  */
local theta = 1			/* Fraction of advertising to be used as investment in brands: 1 means that we take all advertising expenses as investments on brand capital. */


/* Compustat */

* Import Compustat

use  ccm, clear

keep if fyear >= 1975

tab fyr 
tab pddur


* keep companies incorporated in the US
keep if fic == "USA"
*keep those with native currency of US dollar
keep if curcd == "USD"

ren *, lower
ren lpermno permno
g year = fyear

drop if mi(xad) // Drop observations with missing advertising expenses similar as the literature
drop if sale <= 0
drop if mi(at)
drop if at <= 0




	* Exclude financial firms and firms whose sic code is missing

	destring sic, replace force
	destring sich, replace force

	replace sich = sic if mi(sich)				/* Use historical SIC as default but replace it with the current one if it is missing */
	drop if sich >= 6000 & sich <= 6999			/* Exclude financial firms  according to Belo et al (RED2014) */
	drop if sich >= 4900 & sich <= 4999          /* Exclude regulated firms according to Belo et al (RED2014) */
	keep if ~mi(sich)							/* Exclude firms with missing SIC code */
	
	* Fama-French 17 industry classifications

	ffind sich, newvar(ffind17) type(17)
	
* Keep Unique Observation

bys permno year (fyr): keep if _n==_N

xtset permno fyear


tempfile ccm
save `ccm'

* Import CPI data and merge with Compustat

 import excel "cpi.xlsx", sheet("Sheet1") firstrow clear


merge 1:m year using `ccm', keep(2 3) nogen


* Deflate relevant series

foreach var of varlist xad at{
		replace `var' = (`var'/cpi)*100 // 
		}


/* Brand capital */

g brandcap = .
bys permno (year): replace brandcap = (1-`delta')*`theta'*xad/(`g'+`delta') ///
	+ `theta'*xad if _n==1
bys permno (year): replace brandcap = (1-`delta')*brandcap[_n-1] + `theta'*xad ///
	if year==year[_n-1]+1 & _n>1
	
	
drop if mi(brandcap) 

bysort year: egen brand_rank = rank(-brandcap) 

g top_brandcap = 0
replace top_brandcap = 1 if brand_rank <= 100


* Sort portfolios 
if `top_brandcap_pf' == 0{
bys year: egen BK_pf = xtile(brandcap), nq(5) // 
}
* for top brands portfolio based on brand capital
if `top_brandcap_pf' == 1{
	g BK_pf = top_brandcap
}

* Set year and save file

g myear = year + 1 			/* To merge with CRSP */	

keep permno year myear brandcap at ffind17 BK_pf conm brand_rank 

order year brand_rank  brandcap 

sort permno year

cd "`raw_data_path'"

tempfile CCM
save `CCM'

/* CRSP Monthly file */

use crsp, clear 

*rename all variable names to be lowercase
rename *, lower

* keep common shares: share codes 10 and 11
keep if shrcd==10 | shrcd==11

*keep only those from the main exchanges
keep if exchcd==1 | exchcd==2 | exchcd==3

* Set Date

g t = mofd(date)
format t %tm

* Add up different distributions (codes missing divamt as zero) 

replace divamt = 0 if mi(divamt) | divamt < 0
bys permno t: egen aux = sum(divamt)
bys permno t: keep if _n==_N
replace divamt = aux
drop aux

* Turn bid-ask quotes into price

replace prc = abs(prc) 
g ME = prc*shrout
bys permno (t): g ME_L1 = ME[_n-1] if t==t[_n-1]+1

		 
																				 
* Portfolio year

g month = month(date)
g myear = cond(month >= 7, year(date), year(date)-1)

/* Merge CRSP with Compustat */

merge m:1 permno myear using `CCM', keep(3) nogen
sort permno t


* Sample period

keep if myear >= 1975
keep if myear <= 2020


/* REGRESSIONS */

* Construct equal-weight return
if `vw' == 0{

collapse (mean) ret , by(t BK_pf)

drop if BK_pf == .
replace ret = ret*100		/* Percentage term */ 	
}
* Merge factors 

preserve

import delimited "factors.CSV", varnames(1) clear

g year = int(date/100)
g month = mod(date, 100)
g t = ym(year, month)
format t %tm

drop year month date

tempfile factors
save `factors'

restore

merge m:1 t using `factors', nogen keep(1 3)
sort BK_pf t

/* do companies with higher brand capital earn excess returns? */

g exret = ret - rf		/* Excess return */


* Reshpae data

keep BK_pf t ret exret 
reshape wide ret exret, i(t) j(BK_pf)
tsset t

merge 1:1 t using `factors', nogen keep(1 3)

* set output file path
cd "`output_path'"


drop if t >= ym(2021,1)

* BMK Portfolio (5-1) return
if `top_brandcap_pf' == 0{
g BMK = exret5 - exret1
g cons = 1
label var cons "$ E[R] - r_f (\%)$"

eststo:newey BMK mktrf smb hml mom if t >= ym(2000,9), lag(1) 

eststo:newey BMK mktrf smb hml rmw cma mom if t >= ym(2000,9), lag(1) 


** Full sample
eststo:newey BMK mktrf smb hml mom, lag(1) 
eststo:newey BMK mktrf smb hml mom rmw cma, lag(1) 


esttab using "brandcap_hml.csv", replace ///
		t(2) r2 b(2) star(* 0.10 ** 0.05 *** 0.01) ///
		width(\textwidth) label nomtitle nolines compress
eststo clear
}

*********************************************************************************************************************************
** for top brands using brand capital: one dummey: either top brand or not. Full sample
if `top_brandcap_pf' == 1{
g BMK = exret1 - exret0

g cons = 1
label var cons "$ E[R] - r_f (\%)$"


eststo:newey exret1 mktrf smb hml mom if t >= ym(2000,9), lag(1)
eststo:newey exret1 mktrf smb hml rmw cma mom if t >= ym(2000,9), lag(1)

* Full sample
eststo:newey exret1 mktrf smb hml mom, lag(1)
eststo:newey exret1 mktrf smb hml rmw cma mom, lag(1)
 
esttab using "top_brandcap_pf.csv", replace ///
		t(2) r2 b(2) star(* 0.10 ** 0.05 *** 0.01) ///
		width(\textwidth) label nomtitle nolines compress
eststo clear

}
